package com.bird.extension.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: sijunqiang
 * @Date: 2020/4/6 15:54
 * @Description: the function of this class is to parse annotations
 */
public class AnnotationParse {

    /**
     * the method  is to parse the annotations above the class
     */
    public static void parseTypeAnnotation() throws ClassNotFoundException {
        // get special class big Class
        Class clazz = Class.forName("com.bird.extension.annotation.BirdCourse");
        //  get the annotations above the class, instead of the annotations above the method and member variables inside
        Annotation[] ClassTypeAnnotations = clazz.getAnnotations();
        for (Annotation annotation : ClassTypeAnnotations) {
            // strong turn type
            CourseInfoAnnotation courseInfoAnnotation = (CourseInfoAnnotation) annotation;
            System.out.println("curseName: " + courseInfoAnnotation.courseName() + "\n" +
                    "courseTag: " + courseInfoAnnotation.courseTag() + "\n" +
                    "courseProfile: " + courseInfoAnnotation.courseProfile() + "\n" +
                    "courseIndex: " + courseInfoAnnotation.courseIndex() + "\n");
        }
    }


    /**
     * the method is parse the annotations above the member variable
     */
    public static void parseFieldAnnotation() throws ClassNotFoundException {
        // get special class big Class
        Class clazz = Class.forName("com.bird.extension.annotation.BirdCourse");
        // get the object all member variable
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // determines whether there is annotation above the member variable for specified annotation type;
            boolean annotationPresent = field.isAnnotationPresent(PersonInfoAnnotation.class);
            if (annotationPresent) {
                //get the special annotation type annotation a
                PersonInfoAnnotation personInfoAnnotation = field.getAnnotation(PersonInfoAnnotation.class);
                System.out.println("name: " + personInfoAnnotation.name() + "\n" +
                        "age: " + personInfoAnnotation.age() + "\n" +
                        "gender: " + personInfoAnnotation.gender() + "\n");
                //  iterate to get all the languages
                for (String language : personInfoAnnotation.language()) {
                    System.out.println("language: " + language);
                }
            }
        }
    }


    /**
     * the method to parse the annotation above method annotations
     */
    public static void parseMethodAnnotation() throws ClassNotFoundException {
        // get special class big Class
        Class clazz = Class.forName("com.bird.extension.annotation.BirdCourse");
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            // determines  whether there is annotation above the method for special annotation type;
            boolean annotationPresent = declaredMethod.isAnnotationPresent(CourseInfoAnnotation.class);
            if (annotationPresent) {
                CourseInfoAnnotation annotation = (CourseInfoAnnotation) declaredMethod.getAnnotation(CourseInfoAnnotation.class);
                System.out.println("courseName: " + annotation.courseName() + "\n" +
                        "courseTag: " + annotation.courseTag() + "\n" +
                        "courseProfile: " + annotation.courseProfile() + "\n" +
                        "courseIndex: " + annotation.courseIndex());
            }

        }
    }

    /**
     *  test above about class and method ,member variable
     */
    public static void main(String[] args) throws ClassNotFoundException {

        System.out.println("************* print parse class above annotation***********************");
       //// parseTypeAnnotation();

        System.out.println("*****************print parse member  above annotation **************");
        parseFieldAnnotation();

        System.out.println("******************print parse method above annotation ***************************");
       // parseMethodAnnotation();
    }
}
